package com.shengqi.xinnongren.work.ui.home.activities;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.common.MyWebViewActivity;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.home.model.NewsBean;
import com.shengqi.xinnongren.work.ui.home.model.NewsDetailsBean;
import com.shengqi.xinnongren.work.ui.home.presenter.NewsListPresenter;
import com.shengqi.xinnongren.work.ui.home.view.NewsListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NewsListActivity extends BaseMvpActivity<NewsListView, NewsListPresenter> implements NewsListView {


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_news)
    RecyclerView mRvNews;

    private NewsListAdapter adapter;
    private List<NewsBean> list;

    private int pageIndex = 0;

    @Override
    protected NewsListPresenter createPresenter() {
        return new NewsListPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_news_list;
    }

    @Override
    protected void initView() {

        list = new ArrayList<>();
        adapter = new NewsListAdapter(R.layout.item_news_list, list);
        mRvNews.setLayoutManager(new LinearLayoutManager(this));
        mRvNews.setAdapter(adapter);

//        adapter.setOnItemClickListener((adapter, view, position) -> mPresenter.getNewsDetails(list.get(position).getContentId()));
        adapter.setOnItemClickListener((adapter, view, position) -> startActivity(MyWebViewActivity.getIntent(this, list.get(position).getTitle(), list.get(position).getContents())));

        mRefreshLayout.setOnRefreshListener(refreshLayout -> refreshData()).setOnLoadMoreListener(refreshLayout -> mPresenter.getNewsList(pageIndex));
    }

    private void refreshData() {
        list.clear();
        pageIndex = 0;
        mPresenter.getNewsList(pageIndex);
    }

    @Override
    protected void initData() {
        mPresenter.getNewsList(pageIndex);
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        mRefreshLayout.finishRefresh().finishLoadMore();
        hideLoadingView();
    }

    @Override
    public void getNewsListRes(HttpRespond<List<NewsBean>> respond) {
        mRefreshLayout.finishRefresh().finishLoadMore();
        LogUtil.e(respond.toString());
        if (respond.data.size() > 0) {
            pageIndex++;
        }
        list.addAll(respond.data);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void showNewsDetails(NewsDetailsBean data) {
        startActivity(MyWebViewActivity.getIntent(this, data.getTitle(), data.getContents()));
    }

    @Override
    public void showRequestFailed(String message) {
        ToastHelper.getInstance(this).showWarn(message);

    }

    class NewsListAdapter extends BaseQuickAdapter<NewsBean, BaseViewHolder> {

        public NewsListAdapter(int layoutResId, List<NewsBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, NewsBean item) {
            helper.setText(R.id.tv_title, item.getTitle());
        }
    }

}
